﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using library;

namespace MissleDefense
{
    public partial class FormRadar : Form
    {
        String[] ErrorLog = new String[50];
                
        public FormRadar()
        {
            InitializeComponent();
        }
        static string cwdd = System.IO.Directory.GetCurrentDirectory();
        string cwd = System.IO.Directory.GetDirectoryRoot(cwdd);
       
        private void Form1_Load_1(object sender, EventArgs e)
        {

            cwd += "C#\\MD Final\\Files\\";

            toolStripStatusLabel1.Text = "Acquiring Target. . . ";

            lblRadarName.Text = "Car Tracker";

            Status RAD_Status = new Status();             

            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Status));
                TextWriter textWriter = new StreamWriter(cwd + "RAD_Status.xml");
                serializer.Serialize(textWriter, RAD_Status);
                textWriter.Close();
               
            }
            catch (Exception ex)
            {
                //write to the array ErrorLog
            }
        }

        private void exitToolStripMenuAbort_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Mission Aborted");
            Application.Exit();
            
        }

        private void btnRandom_Click(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "Acquiring Target. . . ";

            maskedTextBoxSpeed.ReadOnly = false;

            int speed = library.Utility.RandomNumber (30, 200);
            int distance = library.Utility.RandomNumber (100, 2999);
            int direction = library.Utility.RandomNumber (1, 360);
            int pickTarget = library.Utility.RandomNumber (1, 5);

            if (pickTarget == 1)
            {
                txtName.Text = "Nick's Car";
                radNick.Checked = true;
            }
            if (pickTarget == 2)
            {
                txtName.Text = "Hui's Car";
                radHui.Checked = true;
            }
            if (pickTarget == 3)
            {
                txtName.Text = "Spencers T-Ruck";
                radSpencer.Checked = true;
            }
            if (pickTarget == 4)
            {
                 txtName.Text = "Ceiling Computer";
                 radCeiling.Checked = true;
                 maskedTextBoxSpeed.ReadOnly = true;
                 speed = 0;
            }

            maskedTextBoxDirection.Text = direction.ToString();
            maskedTextBoxDistance.Text = distance.ToString();
            maskedTextBoxSpeed.Text = speed.ToString();
        }

        private void Radar_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                string path2 = cwd + "RAD_Status.xml";
                File.Delete(path2);
            }
            catch (Exception ex)
            {
                //Errorlog
            }



            //Status RAD_Status = new Status();

            //try
            //{
            //    XmlReader reader = new XmlTextReader(cwd + "RAD_Status.xml");
            //    XmlSerializer serializer = new XmlSerializer(typeof(Status));
            //    RAD_Status = (Status)serializer.Deserialize(reader);
            //}
            //catch (Exception ex)
            //{
            //    //Write ex to Log
            //}

            //try
            //{
            //    XmlSerializer serializer = new XmlSerializer(typeof(Status));
            //    TextWriter textWriter = new StreamWriter(cwd + "RAD_Status.xml");
            //    serializer.Serialize(textWriter, RAD_Status);
            //    textWriter.Close();
            //}
            //catch (Exception ex)
            //{
            //    //write to errorlog array
            //}

        }
                    
        private void btnLockOn_Click(object sender, EventArgs e)
        {
            LON RAD_Event = new LON();

         bool checkout = true;

         toolStripStatusLabel1.Text = "Target Acquired. . . Engage";

            if (Utility.NumberRangeValidator(1, 360, maskedTextBoxDirection.Text))
            {
                maskedTextBoxDirection.Clear();
                MessageBox.Show("Enter a direction between 1-360° SOLDIER!");
                checkout = false;
            }

            if (Utility.MaxRangeValue(3000, maskedTextBoxDistance.Text))
            {
                maskedTextBoxDistance.Clear();
                MessageBox.Show("Target beyond maximum range!");
                checkout = false;
            }
            if (checkout)
            {
                if (radNick.Checked)
                {
                    RAD_Event.Targ = LON.TargetList.NicksCar;
                    RAD_Event.ThreatLevel = LON.ThreatList.Medium;
                }
                else if (radHui.Checked)
                {
                    RAD_Event.Targ = LON.TargetList.HuisCar;
                    RAD_Event.ThreatLevel = LON.ThreatList.Low;
                }
                else if (radSpencer.Checked)
                {
                    RAD_Event.Targ = LON.TargetList.SpencersTruck;
                    RAD_Event.ThreatLevel = LON.ThreatList.None;
                }
                else if (radCeiling.Checked)
                {
                    RAD_Event.Targ = LON.TargetList.CeilingComputer;
                    RAD_Event.ThreatLevel = LON.ThreatList.High;
                }

                if (radJavelin.Checked)
                    RAD_Event.Missile = LON.MissileList.Javelin;
                else if (radTomahawk.Checked)
                    RAD_Event.Missile = LON.MissileList.Tomahawk;
                else if (radBunker.Checked)
                    RAD_Event.Missile = LON.MissileList.BunkerBuster;
                else if (radNuke.Checked)
                    RAD_Event.Missile = LON.MissileList.Nuke;

                RAD_Event.Direction = Double.Parse(maskedTextBoxDirection.Text);
                RAD_Event.Distance = Double.Parse(maskedTextBoxDistance.Text);
                RAD_Event.Speed = maskedTextBoxSpeed.Text;

                try
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(LON));
                    TextWriter textWriter = new StreamWriter(cwd + "RAD_Event.xml");
                    serializer.Serialize(textWriter, RAD_Event);
                    textWriter.Close();
                }
                catch (Exception ex)
                {
                    //Write ex to Log
                }
            }
        
        }

        private void helpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormHelp form2 = new FormHelp();
            form2.Show();
        }

     }
  }
